<template>
  <el-dialog v-model="Store.dialogTableVisible" title="公告管理">
    <el-dialog v-model="innerVisible" title="修改公告" append-to-body>
      <el-form :model="form">
        <el-form-item label="公告标题">
          <el-input v-model="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="公告信息">
          <el-input v-model="form.content" autocomplete="off" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="ConfirmEdit">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <el-table :data="paginatedData">
      <el-table-column property="timestamp" label="日期" width="150" />
      <el-table-column property="title" label="标题" width="150" />
      <el-table-column property="content" label="内容" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="success" round @click="editAnnouncement(scope.row)">编辑</el-button>
          <el-button type="danger" round @click="deleteAnnouncement(scope.row.title)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="Store.gridData.length"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="handlePageChange"
    />
  </el-dialog>
</template>

<script setup>
import { ref, computed } from 'vue'
import axios from 'axios'
import { useAnnouncementsStore } from '@/stores/announcements.js'
const Store = useAnnouncementsStore()

const currentPage = ref(1)
const pageSize = ref(5)
const innerVisible = ref(false)
const form = ref({
  title: '',
  content: ''
})
let id = null

const deleteAnnouncement = (title) => {
  console.log(title)
  axios.post('/api/announcements/delete', { title: title }).then((res) => {
    console.log(res.data.result)
    Store.gridData = res.data.result
  })
}

const editAnnouncement = (row) => {
  console.log(Store.gridData)
  form.value.title = row.title
  form.value.content = row.content
  id = Store.gridData.find((item) => item.title === row.title).id
  console.log(id)
  innerVisible.value = true
}

const ConfirmEdit = () => {
  //获取当前时间
  const date = new Date()
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hours = date.getHours()
  let minutes = date.getMinutes()
  if (minutes < 10) {
    minutes = '0' + minutes
  }
  const dateTime = `${year}-${month}-${day} ${hours}:${minutes}`
  axios
    .post('/api/announcements/update', {
      id: id,
      title: form.value.title,
      content: form.value.content,
      timestamp: dateTime
    })
    .then((res) => {
      console.log(res.data.result)
      Store.gridData = res.data.result
      innerVisible.value = false
    })
}

const paginatedData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value
  const end = start + pageSize.value
  return Store.gridData.slice(start, end)
})

const handlePageChange = (newPage) => {
  currentPage.value = newPage
}
</script>

<style scoped></style>
